package com.firstjianmo.broadcasttest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    public static final String TAG = "MainActivity";
    private NetworkChangeReceiver networkChangeReceiver;
    private LocalBroadcastManager localBroadcastManager;
    private LocalReceiver localReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // 动态注册广播接收器 接收网络连接改变
        IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);

        // 本地广播接收器注册 只能通过动态注册的方法注册  并且是通过 本地广播管理器注册
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter localIntentFilter = new IntentFilter("com.firstjianmo.broadcasttest.LOCAL_BROADCAST");
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, localIntentFilter);


        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.firstjianmo.broadcasttest.MY_BROADCAST");
                intent.putExtra("msg","发送了一条来自BroadcastTest的广播信息");
                sendBroadcast(intent);
                //sendOrderedBroadcast(intent, null); //发送有序广播

                // 发起本地广播

                Intent localIntent = new Intent("com.firstjianmo.broadcasttest.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(localIntent);
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);
        localBroadcastManager.unregisterReceiver(localReceiver);
    }

    class NetworkChangeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (null != networkInfo && networkInfo.isAvailable()) {
                Toast.makeText(context, "network is avaliable", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "network is unavaliable", Toast.LENGTH_LONG).show();
            }

        }
    }

    class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received local broadcast", Toast.LENGTH_LONG).show();
        }
    }


}
